class UsersController < ApplicationController
  
  before_filter :login_required, :only => [:update]
  
  # home de usuario
  def home
      @user = User.find_by_login(params[:login])
      render_404 and return if @user.blank?

      @events = @user.events.blank? ? {} : @user.events.find(:all, :conditions => ["starts_at > ?", Time.now])
      @confirmed_events = @user.confirmed_events.find(:all, :conditions => ["starts_at > ?", Time.now])
      @denied_events = @user.denied_events.find(:all, :conditions => ["starts_at > ?", Time.now])
      @pagetitle = @user.public_name
  end

  # render new.rhtml
  def new
    #redirigimos a la home de usuario si está logueado
    redirect_to user_home_url(current_user.login) and return if logged_in?
    @pagetitle = "regístrate en tvienes"
  end

  def create
    #redirigimos a la home de usuario si está logueado
    redirect_to user_home_url(current_user.login) and return if logged_in?
    
    cookies.delete :auth_token
    reset_session
    @user = User.new(params[:user])
    return unless request.post?
    session["activation"] = true

    @user.save!

    # avatar
    unless params[:avatar].blank? || params[:avatar][:file].blank?
      @user.update_avatar(params[:avatar][:file])
    end
    
    redirect_to home_url
    
    flash[:notice] = "Gracias por registrarte :) Te hemos enviado un correo de activación."
  rescue ActiveRecord::RecordInvalid
    render :action => 'new'
  end
  
  def update
    @user = User.find(current_user.id)
    @pagetitle = "actualiza tus datos personales"
    
    return unless request.post?
    @user.update_attributes(params[:user])
    
    # avatar
    unless params[:avatar].blank? || params[:avatar][:file].blank?
      @user.update_avatar(params[:avatar][:file])
    end
    flash[:notice] = "Ya hemos actualizado tus datos!!"
    @pagetitle = "regístrate en tvienes"
    
  end
  
  def activate
    redirect_to user_home_url and return if logged_in?
    self.current_user = params[:activation_code].blank? ? :false : User.find_by_activation_code(params[:activation_code])
    if logged_in? && !current_user.activated?
      current_user.activate
      flash[:notice] = "Acabas de completar tu registro."
      redirect_to user_home_url(current_user.login)
    else
      flash[:alert] = "El código que nos envías es incorrecto."
      redirect_to home_url
    end
  end
  
  # disponibilidad del nombre de usuario
  def availability
    @login = params[:user_login].sanitize(40, true) if params[:user_login]
    newuser = User.new
    newuser.login = @login
    @available = User.count(:conditions => [ "login = ?", @login ]) == 0 && !RESERVED_WORDS.include?(@login) unless params[:user_login].size < 3
    render :layout => false, :partial => 'users/availability'
  end
  
  def add_friend
    @user = User.find(params[:id])
    Friending.create(:friend_id => params[:id], :user_id => current_user.id)
    redirect_to user_home_url(@user.login)
  end
  
  def remove_friend
    @user = User.find(params[:id])
    friending = Friending.find_by_friend_id(params[:id])
    friending.destroy
    redirect_to user_home_url(@user.login)
  end
  

end
